<style lang="less" scoped>
    @import 'register.less';
</style>

<template>
    <div class="main">
        <div style="height: 5%"></div>
        <div class="main-table">
            <div style="height: 10%"></div>
            <Form :model="formData">
                <FormItem>
                    <Input v-model="formData.orgName" clearable size="large" style="width:75%" placeholder="单位名称" @on-blur="checkHave" ></Input>
                </FormItem>
                <FormItem>
                    <Input v-model="formData.orgContacts" clearable size="large" style="width:75%" placeholder="联系人" ></Input>
                </FormItem>
                <FormItem>
                    <Input v-model="formData.orgPhone" clearable size="large" style="width:75%" placeholder="联系电话" ></Input>
                </FormItem>
                <FormItem>
                    <Row>
                        <Col span="15" style="text-align: right">
                            <Input v-model="formData.verCode" clearable size="large" style="width:80%" placeholder="验证码" ></Input>
                        </Col>
                        <Col span="7">
                            <Button type="primary" @click="getCode" :disabled="isDisabled">{{butName}}</Button>
                        </Col>
                    </Row>
                </FormItem>
                <FormItem>
                    <Input v-model="formData.orgEmail" clearable size="large" style="width:75%" placeholder="单位邮箱" ></Input>
                </FormItem>
                <FormItem>
                    <Input v-model="formData.userName" clearable size="large" style="width:75%" placeholder="用户名" @on-blur="checkHave"></Input>
                </FormItem>
                <FormItem>
                    <Input type="password" v-model="formData.userPassword" clearable size="large" style="width:75%" placeholder="密码" ></Input>
                </FormItem>
                <FormItem>
                    <Input type="password" v-model="formData.checkPassword" clearable size="large" style="width:75%" placeholder="确认密码" ></Input>
                </FormItem>
                <FormItem>
                    <Input v-model="formData.orgAddress" clearable size="large" style="width:75%" placeholder="单位地址" ></Input>
                </FormItem>
            </Form>
            <Row>
                <Col span="12">
                    <Button @click="orgSave" type="primary" style="width: 80%" :disabled="isCan">注册</Button>
                </Col>
                <Col span="12">
                    <Button @click="back" type="primary" style="width: 80%">返回</Button>
                </Col>
            </Row>
        </div>
    </div>
</template>

<script>
    import util from '@/libs/util';

    export default {
        name: 'exportable-table',
        data () {
            return {
                data: [],
                formData:[],
                orgName:'',
                interval:null,
                count:60,
                isDisabled:false,
                butName:'获取验证码',
                isCan:false
            };
        },
        mounted() {

        },
        methods: {
            orgSave(){
                if(!(this.checkData())) return;
                let formData = {};
                for (let key in this.formData) {
                    formData[key] = this.formData[key];
                }
                util.ajax.post('/busine/organize/save', util.toFormData(formData)).then(res => {
                    if (res.data.message === 'SUCCESS') {
                        this.$Message.success('保存成功，请等待审核！');
                        this.formData = [];
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                    }
                });
            },
            back(){
                this.$router.go(-1);
            },
            getCode(){
                this.count = 60;
                this.interval = setInterval(() => {
                    this.countDown();
                    }, 1000);
            },
            countDown() {
                if (this.count == 0) {
                    clearInterval(this.interval);
                    this.butName = "发送验证码";
                    this.isDisabled = false;
                } else {
                    this.count--;
                    this.butName = this.count+"秒后重发";
                    this.isDisabled = true;
                }
            },
            checkData(){
                let myreg = /^(((17[0-9]{1})|(13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                let passwords=/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,18}$/;
                let formData = this.formData;
                let boo = true;
                let message = '';
                if(formData.orgName == '' || !(formData.orgName)){
                    message = '请输入单位名称！';
                    boo =  false;
                }else if(formData.orgContacts == '' || !(formData.orgContacts)){
                    message = '请输入联系人！';
                    boo =  false;
                }else if(formData.orgPhone == '' || !(formData.orgPhone)){
                    message = '请输入联系电话！';
                    boo =  false;
                }else if(!myreg.test(formData.orgPhone)){
                    message = '请输入有效联系电话！';
                    boo =  false;
                }else if(formData.userName == '' || !(formData.userName)){
                    message = '请输入用户名！';
                    boo =  false;
                }else if(formData.userPassword == '' || !(formData.userPassword)){
                    message = '请输入密码！';
                    boo =  false;
                }else if(!passwords.test(formData.userPassword)){
                    message = '密码必须包含至少一个字母和6-18个数字组成';
                    boo =  false;
                }  else if(formData.userPassword != formData.checkPassword){
                    message = '两次输入密码不一致！';
                    boo =  false;
                }
                if(!boo){
                    this.$Modal.error({
                        title: '警告',
                        content: message
                    });
                }
                return boo;
            },
            checkHave(that){
                let formData = this.formData
                let _this = this;
                _this.isCan = false;
                if((formData.orgName) && formData.orgName != ''){
                    util.ajax.post('/busine/organize/checkHave', util.toFormData({
                        orgName:formData.orgName
                    })).then(res => {
                        if (res.data.message !== 'SUCCESS') {
                            this.$Modal.error({
                                title: '警告',
                                content: res.data.message
                            });
                            _this.isCan = true;
                        }
                    });
                }

                if((formData.userName) && formData.userName != ''){
                    util.ajax.post('/busine/organize/checkHave', util.toFormData({
                        userName:formData.userName
                    })).then(res => {
                        if (res.data.message !== 'SUCCESS') {
                            this.$Modal.error({
                                title: '警告',
                                content: res.data.message
                            });
                            _this.isCan = true;
                        }
                    });
                }
            }
        }
    };
</script>